package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * Created by lenovo on 2016/7/6.
 */
public class ProjectSearchFragment extends Fragment implements View.OnClickListener {

  protected static final String EXTRA_STARTTIME =
      "com.isunland.managesystem.ui.ProjectSearchFragment.EXTRA_STARTTIME";
  protected static final String EXTRA_ENDTIME =
      "com.isunland.managesystem.ui.ProjectSearchFragment.EXTRA_ENDTIME";
  protected static final String EXTRA_TYPE =
      "com.isunland.managesystem.ui.ProjectSearchFragment.EXTRA_TYPE";
  protected static final String EXTRA_NAME =
      "com.isunland.managesystem.ui.ProjectSearchFragment.EXTRA_NAME";

  private static final int REQUEST_START_TIME = 0;
  private static final int REQUEST_END_TIME = 1;
  private static final int REQUEST_PROJECRT_TYPE = 2;

  private TextView mTvSignStartTime;
  private ImageView mIbSignStartTime;
  private TextView mTvSignEndTime;
  private ImageView mIbSignEndTime;
  private TextView mTvProjectType;
  private ImageView mIbProjectType;
  private EditText mEtProjectName;
  private String mStartTime = "";
  private String mEndTime = "";
  private String mProjectType = "";
  private String mProjectTypeId = "";
  private String mProjectName = "";

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.query);
    CustomerDialog content =
        (CustomerDialog) getArguments().getSerializable(ProjectInfoListFragment.EXTRA_CONTENT);
    mStartTime = content.getName();
    mEndTime = content.getId();
    mProjectType = content.getJobNo();
    mProjectTypeId = content.getMemberCode();
    mProjectName = content.getDept();
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_project_search, container, false);
    findView(v);
    initData();
    return v;
  }

  private void findView(View v) {
    mTvSignStartTime = (TextView) v.findViewById(R.id.tv_sign_startTime);
    mIbSignStartTime = (ImageView) v.findViewById(R.id.ib_sign_startTime);
    mTvSignEndTime = (TextView) v.findViewById(R.id.tv_sign_endTime);
    mIbSignEndTime = (ImageView) v.findViewById(R.id.ib_sign_endTime);
    mTvProjectType = (TextView) v.findViewById(R.id.tv_projectType);
    mIbProjectType = (ImageView) v.findViewById(R.id.ib_projectType);
    mEtProjectName = (EditText) v.findViewById(R.id.et_projectName);
    mIbSignStartTime.setOnClickListener(this);
    mIbSignEndTime.setOnClickListener(this);
    mIbProjectType.setOnClickListener(this);
  }

  private void initData() {
    mTvSignStartTime.setText(mStartTime);
    mTvSignEndTime.setText(mEndTime);
    mTvProjectType.setText(mProjectType);
    mEtProjectName.setText(mProjectName);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      case R.id.menu_item_confirm:
        mProjectName = mEtProjectName.getText().toString();
        CustomerDialog content =
            new CustomerDialog(mStartTime, mEndTime, mProjectType, mProjectTypeId, mProjectName);
        //提交查询条件
        Intent i = new Intent();
        i.putExtra(ProjectInfoListFragment.EXTRA_CONTENT, content);
        getActivity().setResult(Activity.RESULT_OK, i);
        getActivity().finish();
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.ib_sign_startTime:
        showDialog(REQUEST_START_TIME);
        break;
      case R.id.ib_sign_endTime:
        showDialog(REQUEST_END_TIME);
        break;
      //项目类别
      case R.id.ib_projectType:
        showDialog(REQUEST_PROJECRT_TYPE);
        break;
      default:
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;
    switch (requestCode) {
      case REQUEST_START_TIME:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case REQUEST_END_TIME:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case REQUEST_PROJECRT_TYPE:
        dialog = ProjectPropertyDialogFrament.newInstance(ProjectPropertyDialogFrament.kind);
        break;
    }
    FragmentManager fm = getFragmentManager();
    dialog.setTargetFragment(this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_START_TIME) {
      Date date = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mStartTime = MyDateUtil.dateToString(date, MyDateUtil.TYPE_DATE);
      mTvSignStartTime.setText(mStartTime);
    }
    if (requestCode == REQUEST_END_TIME) {
      Date date = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mEndTime = MyDateUtil.dateToString(date, MyDateUtil.TYPE_DATE);
      mTvSignEndTime.setText(mEndTime);
    }
    if (requestCode == REQUEST_PROJECRT_TYPE) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mProjectType = customer.getName();
      mProjectTypeId = customer.getId();
      mTvProjectType.setText(mProjectType);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static ProjectSearchFragment newInstence(CustomerDialog content) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(ProjectInfoListFragment.EXTRA_CONTENT, content);
    ProjectSearchFragment mfragment = new ProjectSearchFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}
